package com.my.study.lock;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: yidujun
 * @create: 2021/03/15 15:48
 */
@Slf4j
public class Main {

    private AtomicInteger atomicInteger = new AtomicInteger(0);

    private static final ScheduledExecutorService SCHEDULER = new ScheduledThreadPoolExecutor(1,
            new BasicThreadFactory.Builder().namingPattern("redisLock-schedule-pool").build());

    {
        SCHEDULER.scheduleAtFixedRate(() -> {
            try {
                int increment = atomicInteger.getAndIncrement();
                if (increment == 3) {
                    int i = 10 / 0;
                }
                log.info("atomicInteger = {}", increment);
            } catch (Exception e) {
                log.error("定时任务异常,原因：", e);
            }
        }, 0, 2, TimeUnit.SECONDS);
    }

    static {
        log.info("static main");
    }

    private static class SonMain {
        {
            System.out.println("sonMain");
        }

        static {
            System.out.println("static sonMain");
        }
    }



    public static void main(String[] args) {
        log.info("=====start=====");
        Main main = new Main();
        log.info("=====end=====");
    }

}
